<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

define('DOMROOT',dirname(dirname(dirname(__FILE__))));
define('CLASSPATH', DOMROOT.'/classes/');
define('APICORE',  CLASSPATH.'/APICore/');

class AnimusSession{
    
    protected $_sessId;
    protected $_userInfo;
    protected $_valid;
    protected $_lastRequest;
    protected $_sessionTime;
    protected $_timeout;



    public function __construct() {
         
//     $this->setSession();
     
    }

    public function setSession(){
        
        $random = rand(0,999999);
        $this->_sessId =   hash(md5,$this->_lastRequest.$random);
        $this->_valid  =   true;
        $this->_lastRequest = $_SERVER['REQUEST_TIME'];
        $this->_sessionTime = $this->_lastRequest;
    }
    
    public function setLastRequestTime(){
        
        $this->_lastRequest = $_SERVER['REQUEST_TIME'];
        
    }

    public function getLastRequestTime(){
        
        return $this->_lastRequest;
        
    }
    
    public function getSessionId(){
        
        return $this->_sessId;
        
    }


    public function setUserInfo($_array){
        
        $this->_userInfo    =   $_array;
        
    }
    
     public function getUserInfo(){
        
        return $this->_userInfo;
        
    }


    public function isValid(){
        
        if($this->_lastRequest - $this->_sessionTime > $this->_timeout){
            
            $this->invalidate();                  
        }
        
        return  $this->_valid;
    }

    public function invalidate(){
        
        $this->_sessId =   NULL;
        $this->_valid = false;
        $this->_userInfo = NULL;
        $this->_lastRequest = null;
        $this->_timeout = 0;
        $this->_sessionTime = 0;
    }
    
    public function setTimeout($timeout){
        
        $this->_timeout = $timeout;
        
    }
    public function manageExistingSession(){
        $settings   =   new SimpleXMLElement(file_get_contents(DOMROOT.'/config/application-settings.xml')); 
        $timeout = ($settings->session->timeout)*60;
        @session_start();
        $session =  $this -> _AnimusResponse -> getSession();
        $session->setSession();
        $session->setTimeOut($timeout);
        return $_SESSION['ASESSION'] = $session;
    }
    public  function getSession(){
        @session_start();
        if(isset($_SESSION['ASESSION'])){
            if($_SESSION['ASESSION']->isValid()){
                $_SESSION['ASESSION']->setLastRequestTime();
                return  $_SESSION['ASESSION'];
            }
            else return null;
        }
        else return null;
    }
    public function closeSession(){ 
       @session_start(); 
        if(isset($_SESSION['ASESSION'])){
            if($_SESSION['ASESSION']->isValid()){
                $_SESSION['ASESSION']->invalidate();
            }
            return true;
        }
    }
}
?>
